package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.CsParts;
import cn.cs.pojo.CsPartsAttribute;
import cn.cs.service.CsPartsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;


@RestController
@RequestMapping(value = "/parts")
public class PartsController {

    @Resource
    private CsPartsService csPartsService;


    /**
     * 分页查询 合作配件
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getPartsByPage")
    public Dto getPartsByPage(Integer pageNumber, Integer pageSize, String partsName, String partsNumber, String factoryId)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("partsName", partsName);
        param.put("partsNumber", partsNumber);
        param.put("factoryId", factoryId);
        return DtoUtil.returnSuccess("获取成功", csPartsService.queryCsPartsPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 分页查询 配件销售情况
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getPartsSellNum")
    public Dto getPartsSellNum(Integer pageNumber, Integer pageSize, String partsName, String partsNumber, String factoryId)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("partsName", partsName);
        param.put("partsNumber", partsNumber);
        param.put("factoryId", factoryId);
        Page<Map<String, Object>> page = csPartsService.getPartsSellNumByMap(param, pageNumber, pageSize);
        return DtoUtil.returnSuccess("获取成功", page);
    }


    /**
     * 新增/编辑 合作配件
     * @param csParts
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateParts")
    @Transactional
    public Dto updateParts(@RequestBody CsParts csParts){
        try {
            String msg = "新增成功";
            String imagesUrl[] = csParts.getImagesUrl().split(",");
            String partsImgs[] = csParts.getPartsImgs().split(",");
            csParts.setImageUrl(imagesUrl[0]);
            if (EmptyUtils.isEmpty(csParts.getId())) {
                csParts.setId(IdWorker.getId());
                csParts.setCreateDate(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                csPartsService.addCsParts(csParts);
            } else {
                csParts.setModifyDate(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                msg = "修改成功";
                csPartsService.modifyCsParts(csParts);
                csPartsService.deleteCsPartsImgByPartsId(csParts.getId());
            }
            List<Map<String, Object>> list = new ArrayList<>();
            for(int i=0;i<imagesUrl.length; i++){
                Map<String, Object> map = new HashMap<>();
                map.put("image_url", imagesUrl[i]);
                map.put("parts_id", csParts.getId());
                map.put("id", IdWorker.getId());
                map.put("type", "2");
                map.put("sort", i);
                list.add(map);
            }
            for(int i=0;i<partsImgs.length; i++){
                Map<String, Object> map = new HashMap<>();
                map.put("image_url", partsImgs[i]);
                map.put("parts_id", csParts.getId());
                map.put("id", IdWorker.getId());
                map.put("type", "1");
                map.put("sort", i);
                list.add(map);
            }
            csPartsService.insertCsPartsImgList(list);
            return DtoUtil.returnSuccess(msg, "");
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 删除 合作配件
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delParts")
    public Dto delParts(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += csPartsService.deleteCsPartsById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 新增/编辑 合作配件属性
     * @param partsAttribute
     * @return
     */
    @RequestMapping(value = "/csPlatform/updatePartsAttribute")
    @Transactional
    public Dto updateParts(@RequestBody CsPartsAttribute partsAttribute){
        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(partsAttribute.getId())) {
                partsAttribute.setId(IdWorker.getId());
                csPartsService.addPartsAttribute(partsAttribute);
            } else {
                msg = "修改成功";
                csPartsService.updatePartsAttribute(partsAttribute);
            }
            return DtoUtil.returnSuccess(msg, "");
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

    /**
     * 删除 合作配件属性
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delPartAttribute")
    public Dto delPartAttribute(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += csPartsService.deletePartsAttribute(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


}
